﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using ZLCMS.Data.Model;
using System.Text;
using System.Collections;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using System.Reflection;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Basics.MessageBoard
{
    public partial class EditReply : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitializeContent();
            }
            else
            {
                Save();
            }
        }

        private void Save()
        {
            FrameHelper frameHelper = new FrameHelper(this);

            MessageReply messageReply;

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                if (replyId.Value == "-1")
                {
                    messageReply = new MessageReply();
                    messageReply.Messageid = int.Parse(uid.Value);
                    messageReply.Content = ta_reply.Value;
                    messageReply.Addtime = DateTime.Now;
                    session.Save(messageReply);
                }
                else
                {
                    messageReply = session.Get<MessageReply>(int.Parse(replyId.Value));
                    messageReply.Content = ta_reply.Value;
                    session.Update(messageReply);
                }

                session.Flush();
            }

            frameHelper.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = frameHelper.NavTabId
            });
        }

        private void InitializeContent()
        {
            uid.Value = Request["uid"];

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                IList<MessageReply> messageReplyList = session.CreateCriteria<MessageReply>().Add(Expression.Eq("Messageid", int.Parse(Request["uid"]))).List<MessageReply>();

                if (messageReplyList.Count == 0)
                {
                    replyId.Value = "-1";
                    return;
                }
                else
                {
                    ta_reply.Value = messageReplyList[0].Content;
                    replyId.Value = messageReplyList[0].Id.ToString();
                }
            }
        }
    }
}
